using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxMenu;
using Indeco.G24.Data;
using Indeco.G24.Services;
using Indeco.G24.Data.SqlClient;
using Indeco.G24.Entities;
using System.Windows.Forms;
using System.Xml;
using System.Net;
//using Indeco.G24.Website.net.webservicex.www1;

namespace Indeco.G24.Website.News
{
    public partial class Default : G24.Web.G24Page
    {
        public string domain =""; 
        public string iddanhmucgoc = "";
        TinTucDanhMucService danhmucService = new TinTucDanhMucService();
        TinTucService tintucService = new TinTucService();
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = "http://" + Request.Url.Host;
            if (Request.Url.Port != null || Request.Url.Port > 0) path += ":" + Request.Url.Port.ToString();
            if (!string.IsNullOrEmpty(Request.ApplicationPath)) path += Request.ApplicationPath;
            domain = path;
            Response.Cache.SetNoStore();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);           
            string action = Request.Params["action"];
            if (!string.IsNullOrEmpty(action))
            {
                if (action.Equals("getWeather"))
                {
                    string city = Request.Params["city"];
                    XmlDocument doc = new XmlDocument();
                    doc.Load("http://vnexpress.net/ListFile/Weather/" + city + ".xml");
                    XmlElement ele = (XmlElement)doc.GetElementsByTagName("AdImg")[0];
                    XmlElement ele1 = (XmlElement)doc.GetElementsByTagName("AdImg1")[0];
                    XmlElement ele2 = (XmlElement)doc.GetElementsByTagName("AdImg2")[0];
                    XmlElement element = (XmlElement)doc.GetElementsByTagName("Weather")[0];
                    string nhietdo1 = ele1.InnerText;
                    string nhietdo2 = ele2.InnerText;
                    string tinhtrang = ele.InnerText;
                    string weather = element.InnerText;
                    string Json = "{\"nhietdo1\":\"" + nhietdo1 + "\",\"nhietdo2\":\"" + nhietdo2 + "\",\"tinhtrang\":\"" + tinhtrang + "\",\"weather\":\"" + weather + "\"}";
                    Response.Write(Json);
                    Response.Flush();
                    Response.End();
                }
                else if (action.Equals("getGold"))
                {
                    WebClient web = new WebClient();
                    string xml = web.DownloadString("http://www.giavang.net/Resources/bind_session.aspx?m=Eximbank");
                    Response.Write(xml);
                    Response.Flush();
                    Response.End();
                }               
            }
            int outcount;            
            TList<TinTucDanhMuc> tin = danhmucService.GetPaged("id_tintucdanhmuc in (select distinct id_tintucdanhmuccha from tb_TinTucDanhMuc)", "Ten ASC", 0, 4, out outcount);
            dataListGetNewsData.DataSource = tin;
            dataListGetNewsData.DataBind();           
            TList<TinTuc> newsTop = tintucService.GetPaged("nhacungcap is null", "thoidiemcapnhat DESC", 0, 1, out outcount);
            DataListTopNews.DataSource = newsTop;
            DataListTopNews.DataBind();    
        }
        protected void dataListGetNewsData_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lb = (Label)e.Item.FindControl("IDDM");
            DataList Ilist = (DataList)e.Item.FindControl("dataList1");
            if (lb != null && Ilist != null)
            {
                TList<TinTucDanhMuc> danhmuc = danhmucService.Find("id_tintucdanhmuccha='" + lb.Text + "'");
                Ilist.RepeatColumns = danhmuc.Count;
                Ilist.DataSource = danhmuc;
                Ilist.DataBind();
            }
            Label text = (Label)e.Item.FindControl("lblCatIDSelected");
            DataList list = (DataList)e.Item.FindControl("dataListNewsTopInCategory");                  
            if (list != null&&text!=null)
            {
                iddanhmucgoc = text.Text;
                int outcount;
                TList<TinTuc> tin = tintucService.GetPaged("id_tintucdanhmuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + text.Text + "') and nhacungcap is null", "thoidiemcapnhat DESC", 0, 1, out outcount);                            
                list.DataSource = tin;
                list.DataBind();
            }
            Label ID = (Label)e.Item.FindControl("IDTinTucDanhMuc");
            ASPxMenu menu = (ASPxMenu)e.Item.FindControl("folderChirld");
            if (ID != null && menu != null)
            {
                TList<TinTucDanhMuc> danhmuccon = danhmucService.Find("id_tintucdanhmuccha='" + ID.Text + "'");
                for(int i=0;i <danhmuccon.Count; i++)
                {
                    menu.Items.Add(danhmuccon[i].Ten.ToString(), "", "", "Category.aspx?IDTinTucDanhMuc="+danhmuccon[i].IDTinTucDanhMuc.ToString());
                }
            }
        }
        protected void dataListNewsTopInCategory_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label text = (Label)e.Item.FindControl("IDTinTuc");           
            DataList list = (DataList)e.Item.FindControl("dataListTitleNewInCategory");
            if (list != null && text != null && iddanhmucgoc != "")
            {
                int outcount;
                TList<TinTuc> tin = tintucService.GetPaged("ID_TinTucDanhMuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + iddanhmucgoc + "') and ID_TinTuc <>'" + text.Text + "' and nhacungcap is null", "thoidiemcapnhat DESC", 0, 6, out outcount);
                list.DataSource = tin;
                list.DataBind();
            }
        }
        protected void DataListTopNews_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label text = (Label)e.Item.FindControl("IDTopNews");
            DataList list = (DataList)e.Item.FindControl("dataListTopOfNews");
            if (text != null && list != null)
            {
                int outcount;
                TList<TinTuc> newsTop = tintucService.GetPaged("nhacungcap is null and id_tintuc <> '" + text.Text + "'", "thoidiemcapnhat DESC", 0, 7, out outcount);
                list.DataSource = newsTop;
                list.DataBind();    
            }
        }
    }
}
